/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.customer;

import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.view.facelets.FaceletContext;
import org.hutech09.jsale.bsn.ICustomerManager;
import org.hutech09.jsale.bsn.exception.ItemExistsException;
import org.hutech09.jsale.bsn.impl.CustomerManager;
import org.hutech09.jsale.entity.Customer;

/**
 *
 * @author abc
 */
@ManagedBean
@SessionScoped
public class cusController implements Serializable {

    @EJB
    private ICustomerManager iCustomerManager;

    private regisModel regismodel;

    private String statusUsername;

    public String getStatusUsername() {
        return statusUsername;
    }

    public void setStatusUsername(String statusUsername) {
        this.statusUsername = statusUsername;
    }

    public regisModel getRegismodel() {
        return regismodel;
    }

    public void setRegismodel(regisModel regismodel) {
        this.regismodel = regismodel;
    }

    private Customer cus;

    public Customer getCus() {

        return cus;
    }

    public void setCus(Customer cus) {
        this.cus = cus;
    }

    public cusController() {
        cus = new Customer();
    }

    public void testIt() {
        if (iCustomerManager.find(cus.getId()) == null) {
            setStatusUsername("User hop le");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "User hop le", ""));
        } else {
            setStatusUsername("User da ton tai");
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "User da ton tai", ""));
        }

    }

    public void login() {
        FacesContext context = FacesContext.getCurrentInstance();
        if (cus.getId() != null && cus.getPassword() != null) {

            if (iCustomerManager.find(cus.getId()) != null && iCustomerManager.find(cus.getId()).getPassword().equals(cus.getPassword())) {
                this.cus = iCustomerManager.find(cus.getId());
                context.addMessage(null, new FacesMessage("Successful", "Hello " + cus.getId()));

            } else {
                cus = new Customer();
                context.addMessage(null, new FacesMessage("Fail", "Please try again! "));
            }
        } else {
            cus = new Customer();
            context.addMessage(null, new FacesMessage("Fail", "Please try again! "));
        }
    }

    /**
     * Creates a new instance of cusController
     */
    public boolean islogin() {
        if(cus.getId()!=null)
               {
                   if(cus.getId().length()>1)
                       return true;
                   else
                       return false;
               }else
                   return false;
    }

    public String register() throws ItemExistsException {

        iCustomerManager.create(cus);
        return "home";
    }

    public void logout() {
        FacesContext context = FacesContext.getCurrentInstance();
        this.cus = null;
        this.cus = new Customer();
        context.addMessage(null, new FacesMessage("Successful", "Logout Successfull!" + cus.getId()));

        // return "SUCCESS"; 
    }
}
